package com.kingschan.util.sql;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author kingschan
 *date:2013-5-21
 */
public class ExpressionScan {
	/**
	 * scan define variable like #{XXX}
	 * @param target_str
	 * @return
	 */
	public static List<String> scanVariable(String target_str){
		List<String> lis = new ArrayList<String>();
		 Pattern p = Pattern.compile("[#{]{2}\\w+[}]{1}");
	        Matcher m = p.matcher(target_str);
	        while(m.find()) {
	            lis.add(m.group());
	        }
		return lis;
		
	}
	
	public static void main(String[] args) {
		String sql ="SELECT hid,title,hrefstr,createdate FROM herftab where 1=1 and (title like '#{title}' ) LIMIT #{page},#{rows}";
		sql="dkfsjdlf dfsklf#{  d d}";
		List<String> lis = ExpressionScan.scanVariable(sql);
		for (int i = 0; i < lis.size(); i++) {
			System.out.println(lis.get(i));
			System.out.println(sql.replace(lis.get(i), "?"));
		}
	}
}
